package com.yang.studentsapp.ui.friends;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.hyphenate.chat.EMClient;
import com.yang.studentsapp.EaseUser;
import com.yang.studentsapp.R;
import com.yang.studentsapp.RecycleViewDivider;
import com.yang.studentsapp.ui.MyAppliction;
import com.yang.studentsapp.ui.adapter.BaseAdapter;
import com.yang.studentsapp.ui.adapter.BaseViewHolder;
import com.yang.studentsapp.utils.EaseCommonUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by ${Ян-Вэнь-Чао} on 2016/10/10.
 * 联系人
 */
public class ContactsFragment extends Fragment {
    RecyclerView recyclerView;
   protected List<EaseUser> contactList = new ArrayList<>();
    private Map<String, EaseUser> contactMap;
    BaseAdapter adapter;
    LinearLayout linearApply;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_contacts, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerView = (RecyclerView) getActivity().findViewById(R.id.contact_recycle);
        linearApply = (LinearLayout) getActivity().findViewById(R.id.conttact_layout_apply);

        getContactList();
        adapter = new BaseAdapter<EaseUser>(getContext(),contactList,R.layout.contact_item) {
            @Override
            protected void convert(Context mContext, BaseViewHolder holder, EaseUser easeUser) {
                holder.setText(R.id.contact_item_name, easeUser.getInitialLetter());
            }
        };
        recyclerView.setAdapter(adapter);
        final LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.addItemDecoration(new RecycleViewDivider(getContext(),
                LinearLayoutManager.HORIZONTAL, 2, getResources().getColor(R.color.gold)));
        linearApply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager manager = getActivity().getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(android.R.id.content, new ApplyFragment());
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });
    }

    public void upDataContact(){
        getContactList();
        adapter.updateData(contactList);
    }

    private void getContactList() {
        contactList.clear();
        contactMap = MyAppliction.getInstance().getContactList();
        if (contactMap == null) {
            return;
        }
        synchronized (this.contactMap) {
            Iterator<Map.Entry<String, EaseUser>> iterator = contactMap.entrySet().iterator();
            List<String> blackList = EMClient.getInstance().contactManager().getBlackListUsernames();
            while (iterator.hasNext()) {
                Map.Entry<String, EaseUser> entry = iterator.next();
                if (!entry.getKey().equals("item_new_friends")
                        && !entry.getKey().equals("item_groups")
                        && !entry.getKey().equals("item_chatroom")
                        && ! entry.getKey().equals("item_robots")) {
                    if (!blackList.contains(entry.getKey())) {
                        EaseUser user = entry.getValue();
                        EaseCommonUtils.setUserInitialLetter(user);
                        contactList.add(user);

                    }
                }
            }
        }
        Collections.sort(contactList, new Comparator<EaseUser>() {
            @Override
            public int compare(EaseUser o1, EaseUser o2) {
                if (o1.getInitialLetter().equals(o2.getInitialLetter())) {
                    return o1.getNick().compareTo(o2.getNick());
                } else {
                    if ("#".equals(o1.getInitialLetter())) {
                        return 1;
                    } else if ("#".equals(o2.getInitialLetter())) {
                        return -1;
                    }
                    return o1.getInitialLetter().compareTo(o2.getInitialLetter());
                }
            }
        });
    }

}
